home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / sphere.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  259 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* Don't take this program too seriously.  It is just a hack. */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14. #include <GL/glut.h>
  15.  
  16. GLfloat light_diffuse[] =
  17. {1.0, 0.0, 0.0, 1.0};
  18. GLfloat light_position[] =
  19. {1.0, 1.0, 1.0, 0.0};
  20. GLUquadricObj *qobj;
  21.  
  22. int win1, win2, submenu1, submenu2;
  23.  
  24. int list = 1;
  25.  
  26. float thetime = 0.0;
  27.  
  28. void
  29. display(void)
  30. {
  31.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  32.   if (glutGetWindow() == win1) {
  33.     glCallList(list);   /* render sphere display list */
  34.   } else {
  35.     glCallList(1);      /* render sphere display list */
  36.   }
  37.   glutSwapBuffers();
  38. }
  39.  
  40. void
  41. display_win1(void)
  42. {
  43.   glPushMatrix();
  44.   glTranslatef(0.0, 0.0, -1 - 2 * sin(thetime));
  45.   display();
  46.   glPopMatrix();
  47. }
  48.  
  49. void
  50. idle(void)
  51. {
  52.   GLfloat light_position[] =
  53.   {1.0, 1.0, 1.0, 0.0};
  54.  
  55.   glutSetWindow(win1);
  56.   thetime += 0.05;
  57.   light_position[1] = 1 + sin(thetime);
  58.   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  59.   display_win1();
  60. }
  61.  
  62. void
  63. delayed_stop(int value)
  64. {
  65.   glutIdleFunc(NULL);
  66. }
  67.  
  68. void
  69. it(int value)
  70. {
  71.   glutDestroyWindow(glutGetWindow());
  72.   printf("menu selection: win=%d, menu=%d\n", glutGetWindow(), glutGetMenu());
  73.   switch (value) {
  74.   case 1:
  75.     if (list == 1) {
  76.       list = 2;
  77.     } else {
  78.       list = 1;
  79.     }
  80.     break;
  81.   case 2:
  82.     exit(0);
  83.     break;
  84.   case 3:
  85.     glutAddMenuEntry("new entry", value + 9);
  86.     break;
  87.   case 4:
  88.     glutChangeToMenuEntry(1, "toggle it for drawing", 1);
  89.     glutChangeToMenuEntry(3, "motion done", 3);
  90.     glutIdleFunc(idle);
  91.     break;
  92.   case 5:
  93.     glutIdleFunc(NULL);
  94.     break;
  95.   case 6:
  96.     glutTimerFunc(2000, delayed_stop, 0);
  97.     break;
  98.   default:
  99.     printf("value = %d\n", value);
  100.   }
  101. }
  102.  
  103. void
  104. init(void)
  105. {
  106.   gluQuadricDrawStyle(qobj, GLU_FILL);
  107.   glNewList(1, GL_COMPILE);  /* create sphere display list */
  108.   gluSphere(qobj, /* radius */ 1.0, /* slices */ 20,  /* stacks 
  109.                                                        */ 20);
  110.   glEndList();
  111.   gluQuadricDrawStyle(qobj, GLU_LINE);
  112.   glNewList(2, GL_COMPILE);  /* create sphere display list */
  113.   gluSphere(qobj, /* radius */ 1.0, /* slices */ 20,  /* stacks 
  114.                                                        */ 20);
  115.   glEndList();
  116.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  117.   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  118.   glEnable(GL_LIGHTING);
  119.   glEnable(GL_LIGHT0);
  120.   glEnable(GL_DEPTH_TEST);
  121.   glMatrixMode(GL_PROJECTION);
  122.   gluPerspective( /* field of view in degree */ 40.0,  /* aspect 
  123.                                                           ratio 
  124.                                                         */ 1.0,
  125.     /* Z near */ 1.0, /* Z far */ 10.0);
  126.   glMatrixMode(GL_MODELVIEW);
  127.   gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
  128.     0.0, 0.0, 0.0,      /* center is at (0,0,0) */
  129.     0.0, 1.0, 0.);      /* up is in positive Y direction */
  130.   glTranslatef(0.0, 0.0, -1.0);
  131. }
  132.  
  133. void
  134. menustate(int inuse)
  135. {
  136.   printf("menu is %s\n", inuse ? "INUSE" : "not in use");
  137.   if(!inuse) {
  138.   }
  139. }
  140.  
  141. void
  142. keyboard(unsigned char key, int x, int y)
  143. {
  144.   if(isprint(key)) {
  145.     printf("key: `%c' %d,%d\n", key, x, y);
  146.   } else {
  147.     printf("key: 0x%x %d,%d\n", key, x, y);
  148.   }
  149. }
  150.  
  151. void
  152. special(int key, int x, int y)
  153. {
  154.    char *name;
  155.  
  156.    switch(key) {
  157.    case GLUT_KEY_F1: name = "F1"; break;
  158.    case GLUT_KEY_F2: name = "F2"; break;
  159.    case GLUT_KEY_F3: name = "F3"; break;
  160.    case GLUT_KEY_F4: name = "F4"; break;
  161.    case GLUT_KEY_F5: name = "F5"; break;
  162.    case GLUT_KEY_F6: name = "F6"; break;
  163.    case GLUT_KEY_F7: name = "F7"; break;
  164.    case GLUT_KEY_F8: name = "F8"; break;
  165.    case GLUT_KEY_F9: name = "F9"; break;
  166.    case GLUT_KEY_F10: name = "F11"; break;
  167.    case GLUT_KEY_F11: name = "F12"; break;
  168.    case GLUT_KEY_LEFT: name = "Left"; break;
  169.    case GLUT_KEY_UP: name = "Up"; break;
  170.    case GLUT_KEY_RIGHT: name = "Right"; break;
  171.    case GLUT_KEY_DOWN: name = "Down"; break;
  172.    case GLUT_KEY_PAGE_UP: name = "Page up"; break;
  173.    case GLUT_KEY_PAGE_DOWN: name = "Page down"; break;
  174.    case GLUT_KEY_HOME: name = "Home"; break;
  175.    case GLUT_KEY_END: name = "End"; break;
  176.    case GLUT_KEY_INSERT: name = "Insert"; break;
  177.    default: name = "UNKONW"; break;
  178.    }
  179.    printf("special: %s %d,%d\n", name, x, y);
  180. }
  181.  
  182. void
  183. mouse(int button, int state, int x, int y)
  184. {
  185.   printf("button: %d %s %d,%d\n", button, state == GLUT_UP ? "UP" : "down", x, y);
  186. }
  187.  
  188. void
  189. motion(int x, int y)
  190. {
  191.   printf("motion: %d,%d\n", x, y);
  192. }
  193.  
  194. void
  195. visible(int status)
  196. {
  197.   printf("visible: %s\n", status == GLUT_VISIBLE ? "YES" : "no");
  198. }
  199.  
  200. void
  201. enter_leave(int state)
  202. {
  203.   printf("enter/leave %d = %s\n",
  204.     glutGetWindow(),
  205.     state == GLUT_LEFT ? "left" : "entered");
  206. }
  207.  
  208. int
  209. main(int argc, char **argv)
  210. {
  211.   qobj = gluNewQuadric();
  212.   glutInit(&argc, argv);
  213.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  214.   win1 = glutCreateWindow("sphere");
  215.   glutEntryFunc(enter_leave);
  216.   init();
  217.   glutDisplayFunc(display_win1);
  218.   glutCreateMenu(it);
  219.   glutAddMenuEntry("toggle draw mode", 1);
  220.   glutAddMenuEntry("exit", 2);
  221.   glutAddMenuEntry("new menu entry", 3);
  222.   glutAddMenuEntry("motion", 4);
  223.   glutAttachMenu(GLUT_LEFT_BUTTON);
  224.   glutCreateMenu(it);
  225.   glutAddMenuEntry("yes", 1);
  226.   glutAddMenuEntry("no", 2);
  227.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  228.   win2 = glutCreateWindow("second window");
  229.   glutEntryFunc(enter_leave);
  230.   glutKeyboardFunc(keyboard);
  231.   glutSpecialFunc(special);
  232.   glutMouseFunc(mouse);
  233. #if 0
  234.   glutMotionFunc(motion);
  235. #endif
  236.   glutVisibilityFunc(visible);
  237.   init();
  238.   light_diffuse[1] = 1;
  239.   light_diffuse[2] = 1;
  240.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  241.   glutDisplayFunc(display);
  242.   submenu1 = glutCreateMenu(it);
  243.   glutAddMenuEntry("submenu a", 666);
  244.   glutAddMenuEntry("submenu b", 777);
  245.   submenu2 = glutCreateMenu(it);
  246.   glutAddMenuEntry("submenu 1", 25);
  247.   glutAddMenuEntry("submenu 2", 26);
  248.   glutAddSubMenu("submenuXXX", submenu1);
  249.   glutCreateMenu(it);
  250.   glutAddSubMenu("submenu", submenu2);
  251.   glutAddMenuEntry("stop motion", 5);
  252.   glutAddMenuEntry("delayed stop motion", 6);
  253.   glutAddSubMenu("submenu", submenu2);
  254.   glutAttachMenu(GLUT_LEFT_BUTTON);
  255.   glutMenuStateFunc(menustate);
  256.   glutMainLoop();
  257.   return 0;             /* ANSI C requires main to return int. */
  258. }
  259.